//
//  YLCertifiedPaymentViewController.m
//  YLFinanceService
//
//  Created by she1323 on 2017/8/29.
//  Copyright © 2017年 she1323. All rights reserved.
//  认证支付

#import "YLCertifiedPaymentViewController.h"
#import "YLCertifiedPaymentViewModel.h"
#import "YLGlobeHeader.h"
#import "YLRealNameModel.h"
#import "YLSwitchRootController.h"
#import "YLRiskAssessmentController.h"
#import "YLMineViewModel.h"
@interface YLCertifiedPaymentViewController ()
@property (nonatomic, weak) UINavigationBar *bar;
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;

/**
 协议
 */
@property (weak, nonatomic) IBOutlet UILabel *agreementLabel;

//@property (weak, nonatomic) IBOutlet UIButton *selectedButton;

@property (nonatomic, strong) YLCertifiedPaymentViewModel *viewModel;

// 身份证
@property (weak, nonatomic) IBOutlet UITextField *idCardTextField;
// 真实姓名
@property (weak, nonatomic) IBOutlet UITextField *nameTextField;
// 新增银行卡号
@property (weak, nonatomic) IBOutlet UITextField *bankCardTextField;
// 手机号
@property (weak, nonatomic) IBOutlet UITextField *phoneNumberTextField;
// 验证码
//@property (weak, nonatomic) IBOutlet UITextField *codeTextField;
// 获取验证码
@property (weak, nonatomic) IBOutlet UIButton *getCodeButton;
@property (weak, nonatomic) IBOutlet UIButton *sureButton;

@end

@implementation YLCertifiedPaymentViewController

- (YLCertifiedPaymentViewModel *)viewModel
{
    if (!_viewModel) {
        _viewModel = [[YLCertifiedPaymentViewModel alloc] init];
    }
    return _viewModel;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"实名认证";
//    _selectedButton.selected = YES;
  //  __weak typeof(self) weakSelf = self;

    
    _scrollView.alwaysBounceVertical = YES;
   // _agreementLabel.attributedText = self.viewModel.agreementAttributedString;
    
    _getCodeButton.layer.masksToBounds = YES;
    _getCodeButton.layer.cornerRadius = 12.5;
    _getCodeButton.layer.borderWidth = 1.0;
    
    _getCodeButton.layer.borderColor = YLLOrgColor.CGColor;
    [_getCodeButton setTitleColor:YLLOrgColor forState:UIControlStateNormal];
    
    RAC(self.viewModel, iDCardText) = _idCardTextField.rac_textSignal;
    
    [_idCardTextField.rac_textSignal subscribeNext:^(NSString * _Nullable x) {
       // NSLog(@"aaaaa");
    }];
    
    RAC(self.viewModel, nameText) = _nameTextField.rac_textSignal;
    RAC(self.viewModel, bankCardText) = _bankCardTextField.rac_textSignal;
    RAC(self.viewModel, phoneNumberText) = _phoneNumberTextField.rac_textSignal;
    
////    RAC(self.viewModel, codeText) = _codeTextField.rac_textSignal;
////    RAC(self.viewModel, selectedAgreement) = RACObserve(_selectedButton, selected);
//    
    RAC(_sureButton, enabled) = self.viewModel.sureEnableSiganl;
    
    [self.viewModel.payCommand.executionSignals.switchToLatest subscribeNext:^(id  _Nullable x) {
        NSLog(@"支付成功");
    }];
    

//    [[_getCodeButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
//        [[weakSelf.viewModel.verificationCodeCommand execute:nil] subscribeNext:^(id  _Nullable x) {
//            [weakSelf.getCodeButton setTitle:x forState:UIControlStateNormal];
//        }] ;
//    }];
//
//    [[self.viewModel.verificationCodeCommand.executing skip:1] subscribeNext:^(NSNumber * _Nullable x) {
//        if ([x boolValue]) {
//            weakSelf.getCodeButton.enabled = NO;
//        } else {
//            weakSelf.getCodeButton.enabled = YES;
//        };
//    }];
    
//    [self.viewModel.verificationCodeCommand.executionSignals.switchToLatest subscribeNext:^(id  _Nullable x) {
//
//
//        [weakSelf.getCodeButton setTitle:x forState:UIControlStateNormal];
//    }];
    
    if (self.navigationController.childViewControllers.count > 1) {
        WHNavBackButton *backButton = [WHNavBackButton navBackButton];
        [backButton setImage:[UIImage imageNamed:@"fanhuijianblack"] forState:UIControlStateNormal];
        [backButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
        self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    }

}

- (void)back
{
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    _bar = self.navigationController.navigationBar;
    [self.navigationController.navigationBar setBarStyle:UIBarStyleDefault];
    [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:YLColor(50, 50, 50)};
    // 修改状态栏颜色
    self.navigationController.navigationBar.barStyle = UIStatusBarStyleDefault;
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [_bar setBackgroundImage:[UIImage imageNamed:@"navBackground"] forBarMetrics:UIBarMetricsDefault];
    // 修改标题颜色
    _bar.titleTextAttributes = @{NSForegroundColorAttributeName: [UIColor whiteColor]};
    // 修改状态栏颜色
    _bar.barStyle = UIStatusBarStyleLightContent;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (IBAction)surePay:(UIButton *)sender {
    
    __weak typeof(self) weakSelf = self;
    [[_viewModel.payCommand execute:nil] subscribeNext:^(id  _Nullable x) {
        [weakSelf loadData];
    }];
}

- (void)loadData
{
    __weak typeof(self) weakSelf = self;
    [[[[YLMineViewModel alloc] init].myInfoCommand execute:nil] subscribeNext:^(id  _Nullable x) {
        // 需要跳到测试
        if (_needToJumpTest) {
            YLRiskAssessmentController *risk = [[YLRiskAssessmentController alloc] init];
            [self.navigationController pushViewController:risk animated:YES];
            [YLSwitchRootController switchMineOrRealName];
        } else {
            // 说明是 我的
            if (weakSelf.navigationController.viewControllers.count == 1) {
                [YLSwitchRootController switchMineOrRealName];
            } else {
                
                [weakSelf.navigationController popViewControllerAnimated:YES];
               
                [YLSwitchRootController switchMineOrRealName];
            }
            
            
        }
    }];
}

//- (void)setRealNameModel:(YLRealNameModel *)realNameModel
//{
//    _realNameModel = realNameModel;
//    self.viewModel.idCardText = _idCardTextField.text = realNameModel.identityId;
//    self.viewModel.nameText = _nameTextField.text = realNameModel.name;
//    self.viewModel.bankCardText = _bankCardTextField.text = realNameModel.bankCardCode;
//}


@end
